package org.streets.database;

import java.util.Properties;
import static org.streets.database.ConnectionConstants.*;
/**
 * Connection parameters configurations 
 * @author dzb
 */
public final class ConnectionConfigurer {
			
    private String _driver = "";
    private String _url = "";
    private String _usr = "";
    private String _pwd = "";
    private String _jndi = "";

    
    public ConnectionConfigurer() {
    	
    }
    
    public ConnectionConfigurer(String jndi) {
    	this._jndi = jndi;
    }
    
    public ConnectionConfigurer(String driver, String url, String user, String pass) {
    	_driver = driver;
    	_url = url;
    	_usr = user;    	
    	_pwd = pass;
    }
    
    
    public ConnectionConfigurer(Properties props) {
    	resolveProperties(props);
    }
    
    
    private String resolvePropertyValue(String flag, Properties props) {
    	for (Object o : props.keySet()) {
    		if (((String)o).indexOf(flag) >= 0) {
    			return props.getProperty((String)o);
    		}
    	}
    	return "";
    }

    public void resolveProperties(Properties props) {
    	this.setDriverName(resolvePropertyValue(CONNECTION_DRIVER, props));
    	this.setUrl(resolvePropertyValue(CONNECTION_URL, props));
    	this.setUser(resolvePropertyValue(CONNECTION_USERNAME, props));
    	this.setPassword(resolvePropertyValue(CONNECTION_PASSWORD, props));
    	this.setJNDI(resolvePropertyValue(CONNECTION_JNDI, props));
    }

    
    public String getDriverName() {
        return _driver;
    }

    public void setDriverName(String driver) {
    	this._driver = driver;
    }

    public String getUrl() {
        return _url;
    }

    public void setUrl(String dbURL) {
        this._url = dbURL;
    }

    public String getUser() {
        return _usr;
    }

    public void setUser(String user) {
        this._usr = user;
    }

    public String getPassword() {
        return _pwd;
    }

    public void setPassword(String passwd) {
        this._pwd = passwd;
    }
    
    public String getJNDI() {
    	return _jndi;
    }
    
    public void setJNDI(String jndi) {
    	this._jndi = jndi;
    }
}
